import { Stick, StickLength } from "../../Script/Game2/Config";
import MathUtil from "../../Script/framework/utils/MathUtil";

/**
 * 线
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Line extends cc.Component {

   
    unuse(){ 
        this.node.zIndex  = 0;
        let rb = this.node.getComponent(cc.RigidBody);
        rb.type = cc.RigidBodyType.Static;
       // this.node.position =cc.v3(0,0);
    } 
    init (config:Stick,balls:cc.Node[]) {  
        let nodeA = balls[config.a]; 
        let nodeB = balls[config.b];  
        this.node.zIndex = -999;
        this.node.position = cc.v3((nodeA.x - nodeB.x)/2,(nodeA.y - nodeB.y)/2);
        
        //线整动态的
        let rb = this.node.getComponent(cc.RigidBody);
        rb.type = cc.RigidBodyType.Dynamic;

        // //A节点掉着线
        // let rjA = nodeA.addComponent(cc.RopeJoint); 
        // rjA.anchor = cc.v2(0.0001,0.0001) 
        // rjA.connectedAnchor = cc.v2(0,StickLength/2) ;
        // //连接刚体
        // rjA.connectedBody = this.node.getComponent(cc.RigidBody);
        // rjA.apply();

        // //线钓着B 
        // let rjB = nodeB.addComponent(cc.RopeJoint); 
        // rjB.anchor =cc.v2(0.0001,0.0001) 
        // rjB.connectedAnchor = cc.v2(0,-StickLength/2) ; 
        // //连接刚体
        // rjB.connectedBody = this.node.getComponent(cc.RigidBody);
        // rjB.apply(); 



         //A节点掉着线
        let rjA = this.node.addComponent(cc.RopeJoint); 
        rjA.anchor = cc.v2(0,StickLength/2 ) 
        rjA.connectedAnchor = cc.v2(0.0001,0.0001);
        //连接刚体
        rjA.connectedBody = nodeA.getComponent(cc.RigidBody);
        //rjA.maxLength = 2
        rjA.apply();

        //线钓着B 
        let rjB = this.node.addComponent(cc.RopeJoint); 
        rjB.anchor = cc.v2(0,-StickLength/2)
         rjB.connectedAnchor =cc.v2(0.0001,0.0001)  ; 
        //连接刚体
        //rjB.maxLength = 2
        rjB.connectedBody = nodeB.getComponent(cc.RigidBody);
        rjB.apply();
    }

     update (dt) {

        //出屏幕强制断开刚体
        if(this.node.y <= -(cc.winSize.height/2)){
            let rjs = this.node.getComponents(cc.RopeJoint)
            for (let index = 0; index < rjs.length; index++) {
                const rj = rjs[index];
                rj.enabled = false;
                cc.tween(this.node).delay(2).call(()=>{this.node.destroy()}).start()
            }
        }

     }
}
